package com.blog.util;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;

import java.io.File;
import java.io.IOException;

public class KeyUtils {
    public ICredential getKey() {
        // JSON文件路径
        String jsonFilePath = "admin/src/main/java/com/blog/util/AK_SK.json";

        try {
            // 创建ObjectMapper实例
            ObjectMapper objectMapper = new ObjectMapper();

            // 读取JSON文件并解析为JsonNode
            JsonNode jsonNode = objectMapper.readTree(new File(jsonFilePath));

            // 从JsonNode中提取信息
            String accessKeyId = jsonNode.get("Access Key Id").asText();
            String secretAccessKey = jsonNode.get("Secret Access Key").asText();

            // 打印提取的信息
            return new BasicCredentials()
                    .withAk(accessKeyId)
                    .withSk(secretAccessKey);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
